# 多阶段构建 Dockerfile
# 适用于 Django 酒店管理系统项目

#############################################
# Builder 阶段：编译代码和安装所有依赖
#############################################
FROM python:3.10-slim as builder

# 设置工作目录
WORKDIR /app

# 复制依赖文件
COPY requirements.txt .

# 安装编译依赖和Python依赖
RUN apt-get update && \
    apt-get install -y --no-install-recommends build-essential default-libmysqlclient-dev pkg-config && \
    pip install --no-cache-dir -i https://pypi.tuna.tsinghua.edu.cn/simple/ -r requirements.txt && \
    # 清理apt缓存以减小镜像体积
    rm -rf /var/lib/apt/lists/*

#############################################
# Runtime 阶段：仅保留运行时所需文件
#############################################
FROM python:3.10-alpine as runtime

# 设置工作目录
WORKDIR /app

# 从builder阶段复制已安装的依赖
COPY --from=builder /usr/local/lib/python3.10/site-packages /usr/local/lib/python3.10/site-packages
COPY --from=builder /usr/local/bin /usr/local/bin

# 复制应用代码
COPY . .

# 创建数据目录
RUN mkdir -p /app/backups /app/logs /app/uploads

# 设置环境变量
ENV PYTHONUNBUFFERED=1

# 暴露端口
EXPOSE 8000

# 启动命令
CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]